<template>
  <div class="main">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="page" class="demo-form-inline" size="mini">
      <el-form-item label="用户名">
        <el-input v-model="page.params.username" placeholder="用户名" clearable />
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="page.params.name" placeholder="姓名" clearable />
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="page.params.nickname" placeholder="昵称" clearable />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="page.params.userEmail" placeholder="邮箱" clearable />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="page.params.sex" placeholder="性别" clearable filterable>
          <el-option label="男" :value="1" />
          <el-option label="女" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" sizi="mini" @click="getByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" class="add-btn" size="mini" @click="addBlog">添加</el-button>
    <el-button type="primary" class="add-btn" size="mini" @click="resetPwd">重置密码</el-button>
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="userId" label="用户编号" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="sex" label="性别" width="70" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1" type="success">男</el-tag>
          <el-tag v-else type="danger">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="header" label="头像" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height: 100px"
            :src="scope.row.header"
            :preview-src-list="[scope.row.header]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="nickname" label="昵称" />
      <el-table-column prop="userEmail" label="邮箱" />
      <el-table-column prop="createdTime" label="创建时间" sortable="custom" />
      <el-table-column prop="updateTime" label="修改时间" sortable="custom" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="warning" @click="handleEdit(scope.row.userId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.userId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <div v-if="showAdd">
      <el-dialog
        title="添加用户"
        :visible.sync="showAdd"
      >
        <user-add @getByPage="getByPage" @closeAddForm="closeAddForm" />
      </el-dialog>
    </div>

    <el-dialog
      title="修改用户信息"
      :visible.sync="showUpdate"
    >
      <user-update :user="user" @getByPage="getByPage" @closeUpdateForm="closeUpdateForm" />
    </el-dialog>

  </div>
</template>

<script>
import myUserApi from '@/api/myUser'
import UserAdd from './user-add'
import UserUpdate from './user-update'
export default {
  components: {
    UserAdd,
    UserUpdate
  },
  data() {
    return {
      showAdd: false, // 控制添加弹窗是否打开
      showUpdate: false, // 控制分类修改是否打开
      userList: [],
      user: {},
      selectionUsers: [], // 当前选中的用户
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [], // 数据
        sortColumn: '', // 排序列
        sortMethod: 'asc' // 排序方式
      }
    }
  },
  created() {
    this.getByPage()
  },
  methods: {
    getByPage() {
      this.page.list = null
      myUserApi.getByPage(this.page).then(resp => {
        console.log(resp.data)
        this.page = resp.data
      })
    },
    handleEdit(id) {
      // 修改
      myUserApi.getById(id).then(resp => {
        this.user = resp.data
        this.showUpdate = true
      })
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除该用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        myUserApi.delete(id).then(resp => {
          this.$message.success(resp.msg)
          this.getByPage()
        })
      })
    },
    addBlog() {
      this.showAdd = true
    },
    closeAddForm() {
      this.showAdd = false
    },
    closeUpdateForm() {
      this.showUpdate = false
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getByPage()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getByPage()
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.sortMethod = e.order
      } else {
        this.page.sortColumn = ''
        this.page.sortMethod = 'asc'
      }
      this.getByPage()
    },
    handleSelectionChange(val) {
      this.selectionUsers = val
    },
    resetPwd() {
      if (this.selectionUsers.length === 0) {
        return
      }
      this.$confirm('是否对选中用户进行密码重置?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const userIds = []
        this.selectionUsers.forEach(u => {
          userIds.push(u.userId)
        })
        myUserApi.resetPwd(userIds).then(resp => {
          this.$message.success(resp.msg)
        })
      })
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 0px 5px 0px;
  }
</style>
